package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;


    /**
     * 微信小程序登录，将微信账户openId跟本地用户关联，返回自定义登陆态(token令牌)
     * @param code 小程序端提交临时凭据，用于服务器端获取微信openId
     * @return {"token":"UUID令牌"}
     */
    @Operation(summary = "微信登陆，返回跟用户关联的token令牌")
    @GetMapping(value = "/wxLogin/{code}")
    public Result<Map<String,String>> wxLogin (@PathVariable String code){
        Map<String,String> codeWithUserInfo = userInfoService.wxLogin(code);
        return Result.ok(codeWithUserInfo);
    }

    @GuiguLogin
    @Operation(summary = "获取登录用户信息")
    @GetMapping(value = "/getUserInfo")
    public Result<UserInfoVo> getUserInfo(){
        Long userId = AuthContextHolder.getUserId();
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }

    @GuiguLogin
    @Operation(summary = "更新用户信息")
    @PostMapping(value = "/updateUser")
    public Result updateUser(@RequestBody UserInfoVo userInfoVo){
        Long userId = AuthContextHolder.getUserId();
        userInfoService.updateUser(userId,userInfoVo);
        return Result.ok();
    }

}
